#******************************************************************
#
# Copyright 2017 Intel Mobile Communications GmbH All Rights Reserved.
#
#-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
#-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
##
# LifX Plugin build script
##

import os
import os.path

Import('env')

target_os = env.get('TARGET_OS')
src_dir = env.get('SRC_DIR')
bridging_path = os.path.join(src_dir, 'bridging')

lifx_env = env.Clone()

print("Reading LifX Plugin script")

def maskFlags(flags):
    flags = [flags.replace('-Wl,--no-undefined', '') for flags in flags]
    return flags


######################################################################
# Build flags
######################################################################

lifx_env.PrependUnique(CPPPATH=[
    '#/resource/c_common/oic_malloc/include',
    '#/resource/c_common/oic_string/include',
    '#/resource/c_common',
    '#/resource/oc_logger/include',
    '#/resource/csdk/logger/include',
    '#/resource/csdk/include',
    '#/resource/csdk/stack/include',
    '#/resource/include',
    '#/extlibs/cjson',
    '#/extlibs/tinycbor/src',
    '#/extlibs/rapidjson/rapidjson/include/rapidjson'
])
lifx_env.AppendUnique(CPPPATH=[
    '#/bridging/include',
    '#/bridging/plugins/lifx_plugin/lifx_objects',
])

if 'g++' in lifx_env.get('CXX'):
    lifx_env.AppendUnique(
        CXXFLAGS=['-std=c++0x', '-Wall', '-Wextra', '-Werror'])

libmpm = lifx_env.get('BUILD_DIR') + 'libmpmcommon.a'
lifx_env['LINKFLAGS'] = maskFlags(env['LINKFLAGS'])
lifx_env.AppendUnique(LINKFLAGS=[
    '-Wl,--allow-shlib-undefined',
    '-Wl,--whole-archive', libmpm,
    '-Wl,-no-whole-archive',
])

lifx_env.AppendUnique(LIBS=[
    'm',
    'octbstack',
    'ocsrm',
    'connectivity_abstraction',
    'coap',
    'curl',
])

#####################################################################
# Source files and Target(s)
######################################################################

lifx_src = [
    'lifxResource.cpp',
    'lifx_objects/lifx.cpp',
]

lifx_env.AppendUnique(LIFX_SRC=lifx_src)
lifxlib = lifx_env.SharedLibrary('lifxplugin', lifx_env.get('LIFX_SRC'))
lifx_env.Depends(lifxlib, libmpm)
lifx_env.InstallTarget(lifxlib, 'lifxplugin')
lifx_env.UserInstallTargetLib(lifxlib, 'lifxplugin')
